#!/usr/bin/env python
# coding: utf-8

"""
bhclasses_define.py
create by cuiyu 2025-08-04
说明：
 * 类型标签定义

用法:
  bhclasses_define.py

更新:
2025-04-04:
"""

import sys
from pathlib import Path
from loguru import logger

_APP_FILE = Path(sys.argv[0])
_APP_ROOT = _APP_FILE.parent

class _BHClassesDef:
    def __init__(self):

        # key=id, val=name
        self.dict_id_name: dict={
            0:  "篦子正常",
            1:  "篦子沉陷",
            2:  "篦子破损",
            3:  "篦子缺失",
            4:  "篦子移位",
            5:  "篦周破损",
            6:  "井盖正常",
            7:  "井盖沉陷",
            8:  "井盖高差",
            9:  "井盖破损",
            10: "井盖缺失",
            11: "井盖凸起",
            12: "井盖移位",
            13: "井周破损",
        }      

        # key=name, val=id
        self.dict_name_id: dict = dict(zip(self.dict_id_name.values(), self.dict_id_name.keys()))  # 需要交换 Key Val

    def getID(self, name: str) -> int:
        try:
            id = int(self.dict_name_id[name])
        except:
            id = -1
            # id = len(self.class_names)
            # self.class_names[name] = id
            # self.class_ids[id] = name
            # logger.error(f"病害类型 “{name}” 不存在，添加新类别，id={id}")
        return id
            
    def getName(self, id: int) -> str:
        try:
            name = self.dict_id_name[id]
        except:
            logger.error(f"病害类型 id={id} 不存在。")
        return name

    def getIDWithAppend(self, name: str) -> int:
        """
        获取类别 id，如果不存在，则添加到 self.class_names 中，需要在 init 时将两个 dict clear()
        
        Args:
          :name:      类别名称
        
        Returns:
          int:       类别 id
        """
        if name in self.dict_name_id :
            _index = self.dict_name_id[name]
        else:
            _index = len(self.dict_name_id)
            self.dict_name_id[name] = _index
            self.dict_id_name[_index] = name
        return _index



BHClasses: _BHClassesDef = _BHClassesDef()



def _main():
    from rich import print

    print(f"总类别：{len(BHClasses.dict_name_id)} 类")
    print(f"类别：{BHClasses.dict_id_name}")

    print(f"类别 2={BHClasses.getName(2)}")
    pass

if __name__ == "__main__":
    # logging.basicConfig(level=logging.DEBUG) 
    _main()